<?php
namespace Com;

class Message{
    private static $APIURL = 'http://sms.253.com/msg/send';
    
    private static $_config = array(
        'un'=>'N8611047',
        'pw'=>'EWkHKShORTd193',
        'phone'=>'',
        'msg'=>'',
        'rd'=>'1',
        'ex'=>''
    );
    
    /**
     * 定制属性设置
     * @param string $name  设置名称
     * @param string $value 设置值
     */
    public static function setConfig($name, $value){
        if(isset(self::$_config[$name])) {
            self::$_config[$name] = $value;
        }
    }
    
    /**
     * 组装短信模板并发送
     * @return string
     */
    public static function send($mobile,$msg,$param = array()){
        //TODO: 初始化

        self::setConfig('phone', $mobile);
        self::setConfig('msg', $msg);
        
        $result = self::curl_http(self::$APIURL, self::$_config);
        //return $result;
        return self::getError($result);
    }
    
    /**
     * 根据状态码返回错误
     * @param string $json
     * @return array
     */
    private function getError($feedback_data){
        //return array('status'=>1,'info'=>'请求成功');
        $result=implode(',', $feedback_data);
        if (empty($feedback_data)) {
            $return['status']=0;
            $return['info'] = '请求失败，未知错误';
            return $return;
        }
        $code = $result[1];        
        switch ($code) {
            case 0:
                $return['status'] = 1;
                $return['info'] = '请求成功';
                break;
            case 101:
                $return['status']=0;
                $return['info']='无此用户';
                break;
            case 102:
                $return['status']=0;
                $return['info']='密码错';
                break;
            case 103:
                $return['status']=0;
                $return['info']='提交过快（提交速度超过流速限制）';
                break;
            case 104:
                $return['status']=0;
                $return['info']='系统忙（因平台侧原因，暂时无法处理提交的短信）';
                break;
            case 105:
                $return['status']=0;
                $return['info']='敏感短信（短信内容包含敏感词）';
                break;
            case 106:
                $return['status']=0;
                $return['info']='消息长度错（>536或<=0）';
                break;
            case 107:
                $return['status']=0;
                $return['info']='包含错误的手机号码';
                break;
            case 108:
                $return['status']=0;
                $return['info']='手机号码个数错（群发>50000或<=0;单发>200或<=0）';
                break;
            case 109:
                $return['status']=0;
                $return['info']='无发送额度（该用户可用短信数已使用完）';
                break;
            case 110:
                $return['status']=0;
                $return['info']='不在发送时间内';
                break;
            case 111:
                $return['status']=0;
                $return['info']='超出该账户当月发送额度限制';
                break;
            case 112:
                $return['status']=0;
                $return['info']='无此产品，用户没有订购该产品';
                break;
            case 113:
                $return['status']=0;
                $return['info']='extno格式错（非数字或者长度不对）';
                break;
            case 115:
                $return['status']=0;
                $return['info']='自动审核驳回';
                break;
            case 116:
                $return['status']=0;
                $return['info']='签名不合法，未带签名（用户必须带签名的前提下）';
                break;
            case 117:
                $return['status']=0;
                $return['info']='IP地址认证错,请求调用的IP地址不是系统登记的IP地址';
                break;
            case 118:
                $return['status']=0;
                $return['info']='用户没有相应的发送权限';
                break;
            case 119:
                $return['status']=0;
                $return['info']='用户已过期';
                break;           
            default:
                $return['status']=0;
                $return['info'] = '请求失败，请联系技术员查询';
                break;
        }
        
        return $return;
    }
    
    /** curl 获取 https 请求
     * @param string $url 请求的url
     * @param array $data 要發送的數據
     * @param array $header 请求时发送的header
     * @param int $timeout 超时时间，默认30s
     */
    private function curl_https($url, $data=array(), $header=array(), $timeout=30){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    
        $response = curl_exec($ch);
    
        curl_close($ch);
    
        return $response;
    }
    
    /** curl 获取 http 请求
     * @param string $url 请求的url
     * @param array $data 要發送的數據
     * @param array $header 请求时发送的header
     * @param int $timeout 超时时间，默认30s
     */
    private function curl_http($url, $data=array(), $header=array(), $timeout=30){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
}
?>